
package com.elong.common.base;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;

import com.elong.common.model.TrainDetail;




public class JavaCrawer {

    public static Parser getParser(String url){
        try {
            Parser parser = new Parser((HttpURLConnection)new URL(url).openConnection());
            parser.setEncoding("UTF-8"); //设置数据源
            return parser;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /** 获得车次详情列表 **/
   public static List<TrainDetail> getElongTrainData(String url){
       List<TrainDetail> detailList = new ArrayList<TrainDetail>();
       
       try {
           Parser parser = getParser(url);
           NodeFilter divFilter = new AndFilter(
                   new TagNameFilter("div"),
                   new HasAttributeFilter("class", "ticketsBox"));
           NodeList nodeList = parser.extractAllNodesThatMatch(divFilter);
           Node divNode = nodeList.elementAt(0);
           NodeList childList = divNode.getChildren();
           int row = 0;
           if(childList != null && childList.size() > 0){
               for(int i = 0; i < childList.size(); i++){
                   Node childNode = childList.elementAt(i);
                   if(childNode instanceof TagNode){  //下面是车站列表 
                       row++;
                       if(row == 1){  //表示是标题栏
                           continue;
                       }
                       TagNode tagNode = (TagNode)childNode;
                       TrainDetail detail = processTextNode(tagNode);
                       if(detail != null)
                           detailList.add(detail);
                   }
               }
           }
  
       } catch (Exception e) {
           e.printStackTrace();
       }
       return detailList;
   }
   
   
   /** 获得车次详情列表 **/
  public static List<TrainDetail> getCtripTrainData(String url){
      List<TrainDetail> detailList = new ArrayList<TrainDetail>();
      
      try {
          Parser parser = getParser(url);
          NodeFilter tableFilter = new AndFilter(
                  new TagNameFilter("table"),
                  new HasAttributeFilter("class", "tb_railway_list"));
          NodeList nodeList = parser.extractAllNodesThatMatch(tableFilter);
          Node tableNode = nodeList.elementAt(0);
          if(tableNode instanceof TableTag){
              System.out.println("ok");
          }
      }catch (Exception e) {
            e.printStackTrace();
      }
      return detailList;
  }
   
   public static TrainDetail processTextNode(TagNode rootNode){
       NodeList childList = rootNode.getChildren();
       Node trainNoNode = childList.extractAllNodesThatMatch(new AndFilter(
               new TagNameFilter("li"),
               new HasAttributeFilter("class","t_no")
               ),true).elementAt(0);
       if(trainNoNode == null) return null;
       TrainDetail detail = new TrainDetail();
       detail.setTrainNo(trainNoNode.getChildren().elementAt(1).toPlainTextString());
       Node trainGoNode = trainNoNode.getNextSibling().getNextSibling().getNextSibling().getNextSibling();
       detail.setStartTime(trainGoNode.getChildren().elementAt(1).toPlainTextString());
       detail.setEndTime(trainGoNode.getChildren().elementAt(3).toPlainTextString());
       
       Node trainStationNode = trainGoNode.getNextSibling().getNextSibling();
       detail.setStartStation(trainStationNode.getChildren().elementAt(1).toPlainTextString());
       detail.setEndStation(trainStationNode.getChildren().elementAt(3).toPlainTextString());
       
       Node trainTimeNode = trainStationNode.getNextSibling().getNextSibling();
       
       detail.setCostTime(trainTimeNode.getChildren().elementAt(1).toPlainTextString());
       
       Node trainPriceNode = trainTimeNode.getNextSibling().getNextSibling();
       Map<String, String> priceMap = new HashMap<String, String>();
       for(int i = 0; i < trainPriceNode.getChildren().size(); i++){
           Node pNode = trainPriceNode.getChildren().elementAt(i);
           if(pNode instanceof TagNode){
               priceMap.put(pNode.getChildren().elementAt(1).toPlainTextString(),
                       pNode.getChildren().elementAt(2).toPlainTextString().replace("￥", ""));
           }
       }
       detail.setSeatPriceMap(priceMap);
       
       return detail;
   }
   

   public static void main(String[] args) {
       List<TrainDetail> detailList = JavaCrawer.getCtripTrainData("http://trains.ctrip.com/TrainBooking/Search.aspx?from=beijing&to=shanghai&day=3");
       Map<String, String> priceMap = null;
       for(TrainDetail detail : detailList){
           System.out.print(detail.getTrainNo() + "  " + detail.getStartTime() + "  " + detail.getEndTime()
                   + "  " + detail.getStartStation() + ":" +
                   detail.getEndStation() + "   " + detail.getCostTime() + "   ");
           priceMap = detail.getSeatPriceMap();
           for(String key : priceMap.keySet()){
               System.out.print(key + ":" + priceMap.get(key) +";");
           }
           System.out.println();
       }
       
       
   }
    
}
